//
//  SecondViewController.m
//  lehuotest
//
//  Created by ice on 2017/1/11.
//  Copyright © 2017年 QF. All rights reserved.
//

#import "SecondViewController.h"
//#import <HMSegmentedControl.h>
#import "LHSegMentControl.h"
#import "HomeGetAttrModel.h"
#import "LHSkuView.h"
//#import "UIViewController+KNSemiModal.h"
#import "UIViewController+Sunken.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface SecondViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *bigCollectionView;

//@property (nonatomic, strong) HMSegmentedControl *segmentedControl;

@property (nonatomic, strong) UIView *topView;

@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) UIButton *sortBtn;

@property (nonatomic, strong) LHSegMentControl *segMent;

@property (nonatomic, assign) CGFloat historyX;
@end

@implementation SecondViewController

-(NSArray *) titles
{
    if (!_titles){
        _titles = [NSArray array];
    }
    return _titles;
}
/*
-(HMSegmentedControl *)segmentedControl
{
    if (!_segmentedControl){
//        self.titles = @[ @"首页", @"新闻", @"体育体育体育", @"经济", @"游戏", @"军事", @"房产"];
        HMSegmentedControl *segmentedControl1 = [[HMSegmentedControl alloc] initWithSectionTitles:self.titles];
        
        _segmentedControl = segmentedControl1;
        segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        segmentedControl1.frame = CGRectMake(10, 0, SCREEN_WIDTH-50, 40);
        
        segmentedControl1.selectionIndicatorHeight = 2;
//        segmentedControl1
        //类型  text image image&text  default:text
        segmentedControl1.type = HMSegmentedControlTypeText;
        //item之间的间隔
        segmentedControl1.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10);
        //设置宽度自适应
        segmentedControl1.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleDynamic;
        //模式
        segmentedControl1.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
        //Indicator位置
        segmentedControl1.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
        //分割线设置
        segmentedControl1.verticalDividerEnabled = NO;
        //分割线颜色
        segmentedControl1.verticalDividerColor = [UIColor blackColor];
        //分割线宽度
        segmentedControl1.verticalDividerWidth = 1.0f;
        //设置字体
        [segmentedControl1 setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
            NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:14]}];
            if (!selected){
                return attString;
            }
            NSAttributedString *seletAttString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:14]}];
            return seletAttString;
        }];
        [segmentedControl1 setIndexChangeBlock:^(NSInteger index) {
            [self.bigCollectionView setContentOffset:CGPointMake(SCREEN_WIDTH *index, 0)];

        }];
    }
    return _segmentedControl;
}
*/

-(LHSegMentControl *)segMent
{
    if (!_segMent){
        _segMent = [[LHSegMentControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        _segMent.isShowSortBtn = YES;
        _segMent.IsShowIndicatorView = YES;
        _segMent.textScale = 0.1;
        _segMent.indicatorColor = [UIColor blueColor];
        _segMent.titles = self.titles;
    }
    return _segMent;
}

-(UIView *)topView
{
    if (!_topView){
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        _topView.backgroundColor = [UIColor whiteColor];
//        [_topView addSubview:self.segmentedControl];
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];
        leftView.backgroundColor = [UIColor whiteColor];
        leftView.layer.shadowColor = [UIColor whiteColor].CGColor;
        leftView.layer.shadowOpacity = 1;
        leftView.layer.shadowRadius = 5;
        leftView.layer.shadowOffset = CGSizeMake(10, 0);
        [_topView addSubview:leftView];
        
    }
    return _topView;
}

-(UIButton *)sortBtn
{
// ks_home_plus
    if (!_sortBtn){
        _sortBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sortBtn.frame = CGRectMake(self.topView.frame.size.width-self.topView.frame.size.height, 0, self.topView.frame.size.height, self.topView.frame.size.height);

        [_sortBtn setImage:[UIImage imageNamed:@"ks_home_plus"] forState:UIControlStateNormal];
        _sortBtn.backgroundColor = [UIColor whiteColor];
        _sortBtn.layer.shadowColor = [UIColor whiteColor].CGColor;
        _sortBtn.layer.shadowOpacity = 1;
        _sortBtn.layer.shadowRadius = 5;
        _sortBtn.layer.shadowOffset = CGSizeMake(-10, 0);
    }
    return _sortBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.titles = @[@"首页", @"新闻新闻新闻", @"体体育育", @"经经济济济", @"游戏", @"军事", @"房产",@"首页", @"新闻", @"体育", @"经济", @"游戏", @"军事", @"房产"];

    [self loadUI];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 100, 40);
    [rightBtn setTitle:@"购物车" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightBtn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = right;
    
}
-(void) next
{
    
    HomeGetAttrModel *model = [[HomeGetAttrModel alloc] init];
    model.attr_id = 1;
    model.attr_name = @"樵夫";
    HomeAttValuesModel *item1 = [[HomeAttValuesModel alloc] init];
    item1.label = @"asdhfjkasdfjjjjj";
    item1.price = @"11.11";
    item1.ID = 2;
    
    HomeAttValuesModel *item2 = [[HomeAttValuesModel alloc] init];
    item2.label = @"222222";
    item2.price = @"222.222";
    item2.ID = 2;
    model.values = @[item1,item2,item1,item1,item1];
    
    
    
    LHSkuView *skuV = [LHSkuView loadFromNib];
    skuV.dataSource = @[model,model,model,model,model];
    skuV.frame = CGRectMake(0, 0, SCREEN_WIDTH, 400);
    skuV.sureBtnTap = ^{
        [self addAnimations];
    };
    
    [self presentSunkenView:skuV];
    
}



- (void)pop {
    // 2.window上添加弹框
    HomeGetAttrModel *model = [[HomeGetAttrModel alloc] init];
    model.attr_id = 1;
    model.attr_name = @"樵夫";
    HomeAttValuesModel *item1 = [[HomeAttValuesModel alloc] init];
    item1.label = @"asdhfjkasdfjjjjj";
    item1.price = @"11.11";
    item1.ID = 2;
    
    HomeAttValuesModel *item2 = [[HomeAttValuesModel alloc] init];
    item2.label = @"222222";
    item2.price = @"222.222";
    item2.ID = 2;
    model.values = @[item1,item2,item1,item1,item1];
    LHSkuView *skuV = [LHSkuView loadFromNib];
    skuV.dataSource = @[model,model,model,model,model];
    skuV.frame = CGRectMake(0, SCREEN_HEIGHT+40, SCREEN_WIDTH, 400);
    skuV.sureBtnTap = ^{
        [self addAnimations];
    };
    [self presentSunkenView:skuV];

    
    
    
}
-(void)addAnimations
{
    UIImageView *cartAnimView=[[UIImageView alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT-420, 100, 100)];
    cartAnimView.image = [UIImage imageNamed:@"icon_child_01"];
    cartAnimView.backgroundColor = [UIColor redColor];
    [[UIApplication sharedApplication].keyWindow addSubview:cartAnimView];
    
    
    
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 11 ];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 0;
    
    //这个是让旋转动画慢于缩放动画执行
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [cartAnimView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    });
    
    [UIView animateWithDuration:1 animations:^{
        cartAnimView.frame=CGRectMake(SCREEN_WIDTH-55, -(SCREEN_HEIGHT - CGRectGetHeight([UIApplication sharedApplication].keyWindow.frame) - 40), 0, 0);
        cartAnimView.transform = CGAffineTransformRotate(cartAnimView.transform, M_PI_2);
    } completion:^(BOOL finished) {
        [cartAnimView removeFromSuperview];
        //动画完成后做的事
        [self dismissSunkenModalView];
    }];
}



-(void) loadUI
{
//    [self.topView addSubview:self.sortBtn];
//    [self.view addSubview:self.topView];
//    [self.view addSubview:self.segmentedControl];
    [self.view addSubview:self.segMent];
    
    __weak SecondViewController *weakVC = self;
    self.segMent.pageChangeBlock = ^(NSInteger index){
        CGFloat offsetX = index * self.view.frame.size.width;
        weakVC.bigCollectionView.contentOffset = CGPointMake(offsetX, 0);
    };
    [self.view addSubview:self.bigCollectionView];

}


- (UICollectionView *)bigCollectionView
{
    if (_bigCollectionView == nil) {
        // 高度 = 屏幕高度 - 导航栏高度64 - 频道视图高度44
        CGFloat h = SCREEN_HEIGHT - 64 - self.topView.frame.size.height ;
        CGRect frame = CGRectMake(0, self.topView.frame.size.height, SCREEN_WIDTH, h);
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _bigCollectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
        _bigCollectionView.backgroundColor = [UIColor whiteColor];
        _bigCollectionView.bounces = NO;
        _bigCollectionView.delegate = self;
        _bigCollectionView.dataSource = self;
        [_bigCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
        
        // 设置cell的大小和细节
        flowLayout.itemSize = _bigCollectionView.bounds.size;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        _bigCollectionView.pagingEnabled = YES;
        _bigCollectionView.showsHorizontalScrollIndicator = NO;
    }
    return _bigCollectionView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionViewDelegate && UICollectionViewDataSource
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.titles.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    UIPageViewController
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    
    [self addChildViewController:vc];
    [cell.contentView addSubview:vc.view];
    
    return cell;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.historyX = scrollView.contentOffset.x;
    self.segMent.historyX = self.historyX;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    [self.segMent titleBtnSelectedWithScrollView:scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.segMent titleBtnColorGradualChangeScrollViewDidScroll:scrollView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
